<template>
  <div class="container">
    <el-row>
      <el-col :span="24">
        <div class="clock-container">
          <div class="clock">
            <ul>
              <li id="hours">05</li>
              <li id="point">:</li>
              <li id="min">20</li>
              <li id="point">:</li>
              <li id="sec">30</li>
              <li id="Date">Monday 26 September 2024</li>
            </ul>
          </div>
        </div>
      </el-col>

      <el-col :span="12" class="left-column">
        <div class="left-container">
          <div class="content-panel header-panel">
            <div class="content">
              <h4 class="section-title">Current Classes</h4>
            </div>
            <div class="button" v-if="noticedata.isAdmin"></div>
          </div>

          <div class="course-container" v-for="item in coursedata.list">
            <div class="course-header">
              <el-icon class="course-icon"><Tickets /></el-icon>
              <span class="course-name">{{item.name}}</span>
            </div>
            <div class="lesson-container">
              <div class="lesson-panel" v-for="lession in item.lessionBean">
                <div class="lesson-content">
                  <p class="lesson-title">{{lession.name}}</p>
                  <p class="lesson-time">{{lession.starttime}}</p>
                </div>
                <div class="lesson-actions">
                  <a :href="'/#/liveStu?courseid='+item.id+'&lessionid='+lession.id" target="_blank">
                    <el-button type="primary" :icon="DataAnalysis" circle class="action-btn"/>
                  </a>
                  <a :href="'/live.html?courseid='+item.id+'&lessionid='+lession.id" target="_blank" v-if="item.username===username">
                    <el-button type="success" :icon="Link" circle class="action-btn"/>
                  </a>
                </div>
              </div>
            </div>
          </div>
        </div>
      </el-col>

      <el-col :span="12" class="right-column">
        <div class="content-panel header-panel">
          <div class="content">
            <h4 class="section-title">Announcements</h4>
          </div>
          <div class="button" v-if="noticedata.isAdmin">
            <el-button type="primary" :icon="Plus" circle @click="noticeshowdialog()" class="add-btn"/>
          </div>
        </div>

        <div class="notice-container">
          <div class="notice-card" v-for="item in noticedata.list">
            <div class="notice-top">
              <h5 class="notice-title">{{item.title}}</h5>
              <el-button type="primary" :icon="Edit" circle  @click="doEdit(item)" v-if="noticedata.isAdmin"/>
            </div>
            <div class="notice-content">
              <p class="notice-text">{{item.content}}</p>
            </div>
            <div class="notice-footer">
              <span class="notice-time"><label v-if="item.name != null">{{item.name}} - </label>{{item.createtime}}</span>
              <el-button type="danger" :icon="Delete" circle @click="doDeleteNotice(item.id)"
                         v-if="noticedata.isAdmin" class="delete-btn"/>
            </div>
          </div>
        </div>

        <div class="view-more">
          <a href="/public#/notice" class="more-link">
            <el-icon><MoreFilled /></el-icon>
            <span>View More</span>
          </a>
        </div>
      </el-col>

      <el-col :span="24" class="notification-section">
        <h4 class="section-title">Notifications</h4>
        <div class="timeline-container">
          <el-timeline>
            <el-timeline-item
                v-for="(item, index) in coursenoticedata.list"
                :key="index"
                :timestamp="formatTimestamp(item)"
                placement="top"
                class="timeline-item"
            >
              <div class="notification-item">
                <strong>{{ item.title }}</strong>: {{ item.content }}
              </div>
            </el-timeline-item>
          </el-timeline>
        </div>
      </el-col>
    </el-row>
          <!-- <div class="ai-chat-button" @click="openChat">
            <img src="../../../src/assets/image/ai-icon.jpg" alt="AI Chat" />
            <span>AI客服</span>
          </div> -->
  </div>
  <!-- <chat-box v-if="dialogFormVisible.isVisible"/> -->

  <!-- notice dialog -->
  <el-dialog v-model="noticedialogFormVisible" :title="onenotice.id ? 'Edit Announcement' :'Save Announcement'" width="500" class="notice-dialog">
    <el-form>
      <el-form-item label="Title" :label-width="formLabelWidth">
        <el-input v-model="onenotice.id" type="hidden"/>
        <el-input v-model="onenotice.title" autocomplete="off" placeholder="Enter announcement title"  maxlength="100" show-word-limit/>
      </el-form-item>
      <el-form-item label="Content" :label-width="formLabelWidth">
        <el-input v-model="onenotice.content" type="textarea" :rows="6" placeholder="Enter announcement content" maxlength="200" show-word-limit/>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="noticedialogFormVisible = false" class="cancel-btn">Cancel</el-button>
        <el-button type="primary" @click="doSubmitNotice()" class="confirm-btn">
          Confirm
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>


//import TopHeader from '../../components/TopHeader.vue' // 引用Header组件
//import TopBottom from '../../components/TopBottom.vue' // 引用Header组件
import {
  DataAnalysis,
  Link,
  Delete,
  Edit,
  Message,
  Plus,
  Star,
} from '@element-plus/icons-vue'
import { reactive,ref,onMounted,onUnmounted} from 'vue'
import { getSysNoticeListLimit,getAllCourseNoticeList,isAdmin,saveNotice,deleteNotice } from '../../api/notice'
import { getLiveLession } from '../../api/course'
import { useStaffStore } from '../../stores/staff'
import ChatBox from './ChatBox.vue'; // 确保路径正确


const staffStore = useStaffStore()
const username = staffStore.username;

const noticedialogFormVisible = ref(false)
const formLabelWidth = '140px'

const coursedata = reactive({
  list:[]
})
// const dialogFormVisible = reactive({
//   isVisible:false
// })

const noticedata = reactive({
  list:[],
  isAdmin:false
})
const coursenoticedata = reactive({
  list:[]
})
const onenotice = reactive({
  id:null,
  title:'',
  content:'',
  courseid:null
})

const formatTimestamp = (item) => {
  // 如果 name 存在且不为空字符串，则拼接 name 和 createtime
  if (item.name) {
    return `${item.name} - ${item.createtime}`;
  }
  // 否则只返回 createtime
  return item.createtime;
}
//时间定时器
var inter
onMounted(() => {
  loaddata();
})
onUnmounted(() => {
  clearInterval(inter);
})
// function openChat() {
//       // 这里可以添加打开聊天窗口的逻辑
//       console.log('AI客服被点击');
//       console.log(dialogFormVisible.isVisible)
//       if(dialogFormVisible.isVisible==false){
//         dialogFormVisible.isVisible=true
//       }else if(dialogFormVisible.isVisible==true){
//         dialogFormVisible.isVisible=false
//     }
//   }

function loaddata()
{
  inter = setInterval(clock,400);
  isAdmin().then((res) => {
    if (res.code == 200)
    {
      noticedata.isAdmin = res.data;
    }
  })
  getSysNoticeListLimit().then((res) => {
    if (res.code == 200)
    {
      noticedata.list = res.data;
    }
  })
  getAllCourseNoticeList().then((res) => {
    if (res.code == 200)
    {
      coursenoticedata.list = res.data;
    }
  })
  getLiveLession().then((res) => {
    if (res.code == 200)
    {
      coursedata.list = res.data;
    }
  })
}
function doSubmitNotice()
{
  noticedialogFormVisible.value = false
  saveNotice(onenotice).then((res) => {
    if (res.code == 200)
    {
      //更新下视图数据
      loaddata();
    }
  })
}
function doEdit(item){
  noticedialogFormVisible.value = true
  onenotice.id=item.id
  onenotice.title=item.title
  onenotice.content=item.content
}
function noticeshowdialog()
{
  noticedialogFormVisible.value = true
  onenotice.id=null
  onenotice.title=''
  onenotice.content=''
}
function doDeleteNotice(id)
{
  deleteNotice({"id":id}).then((res) => {
    if (res.code == 200)
    {
      //更新下视图数据
      loaddata();
    }
  })
}
function clock(){
  var monthNames = [ "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December" ];
  var dayNames= ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
  var today = new Date();
  document.getElementById('Date').innerHTML = (dayNames[today.getDay()] + " " +
      today.getDate() + ' ' + monthNames[today.getMonth()] + ' ' +today.getFullYear());
  var h = today.getHours();
  var m = today.getMinutes();
  var s = today.getSeconds();
  var day = h<11 ? 'AM': 'PM';
  h = h<10? '0'+h: h;
  m = m<10? '0'+m: m;
  s = s<10? '0'+s: s;
  document.getElementById('hours').innerHTML = h;
  document.getElementById('min').innerHTML = m;
  document.getElementById('sec').innerHTML = s;
}

</script>

<style lang="less" scoped>
.ai-chat-button {
  position: fixed;
  right: 20px;
  bottom: 20px;
  background-color: #007bff;
  color: white;
  padding: 10px 20px;
  border-radius: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 5px rgba(0,0,0,0.2);
}
.ai-chat-button img {
  width: 24px;
  height: 24px;
  margin-right: 8px;
}

.container {
  max-width: 100%;
  margin: 0 auto;
  width: 100%;
  overflow: hidden;
  padding: 20px;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  box-sizing: border-box;
}

.clock-container {
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  border-radius: 12px;
  padding: 15px;
  margin-bottom: 20px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.clock {
  color: #2c3e50;
  display: flex;
  justify-content: center;
  align-items: center;

  ul {
    list-style: none;
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 0;
    padding: 0;

    li {
      font-size: 24px;
      font-weight: 600;

      &#Date {
        font-size: 18px;
        margin-left: 20px;
        color: #5a6a85;
      }

      &#point {
        animation: blink 1s infinite;
      }
    }
  }
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

.left-column {
  padding-right: 15px;
  border-right: 1px solid #e0e0e0;
}

.right-column {
  padding-left: 15px;
}

.section-title {
  color: #34495e;
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

.header-panel {
  padding: 12px 15px;
  background-color: #f8f9fa;
  border-radius: 8px;
  margin-bottom: 15px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.left-container {
  height: 500px;
  overflow-y: auto;
  padding-right: 10px;

  &::-webkit-scrollbar {
    width: 6px;
  }

  &::-webkit-scrollbar-thumb {
    background-color: #c1c1c1;
    border-radius: 3px;
  }
}

.course-container {
  margin-bottom: 20px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  overflow: hidden;
}

.course-header {
  display: flex;
  align-items: center;
  padding: 12px 15px;
  background-color: #f1f5f9;
  border-bottom: 1px solid #e2e8f0;

  .course-icon {
    color: #4f46e5;
    margin-right: 10px;
    font-size: 18px;
  }

  .course-name {
    font-weight: 600;
    color: #334155;
  }
}

.lesson-container {
  padding: 10px;
}

.lesson-panel {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px;
  margin-bottom: 8px;
  background-color: #f9fafb;
  border-radius: 6px;
  transition: all 0.2s ease;

  &:hover {
    background-color: #f0f4f8;
    transform: translateY(-2px);
  }

  .lesson-content {
    .lesson-title {
      margin: 0;
      font-weight: 500;
      color: #1e293b;
    }

    .lesson-time {
      margin: 4px 0 0;
      font-size: 13px;
      color: #64748b;
    }
  }

  .lesson-actions {
    display: flex;
    gap: 8px;

    .action-btn {
      transition: transform 0.2s;

      &:hover {
        transform: scale(1.1);
      }
    }
  }
}

.notice-container {
  margin-bottom: 15px;
}

.notice-card {
  background-color: white;
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 12px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
  border-left: 4px solid #4f46e5;
  transition: all 0.2s ease;

  &:hover {
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
  }


  .notice-top {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .notice-title {
      margin: 0 0 8px;
      color: #1e293b;
      font-size: 16px;
    }


    .delete-btn {
      &:hover {
        background-color: #fef2f2;
      }
    }
  }

  .notice-content {
    margin-bottom: 12px;

    .notice-title {
      margin: 0 0 8px;
      color: #1e293b;
      font-size: 16px;
    }

    .notice-text {
      margin: 0;
      color: #475569;
      font-size: 14px;
      line-height: 1.5;
    }
  }

  .notice-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .notice-time {
      font-size: 12px;
      color: #94a3b8;
      font-weight: bold;
    }

    .delete-btn {
      &:hover {
        background-color: #fef2f2;
      }
    }
  }
}

.view-more {
  text-align: center;
  margin-top: 15px;

  .more-link {
    display: inline-flex;
    align-items: center;
    color: #4f46e5;
    text-decoration: none;
    font-size: 14px;
    transition: color 0.2s;

    &:hover {
      color: #4338ca;

      .el-icon {
        transform: translateX(3px);
      }
    }

    .el-icon {
      margin-right: 5px;
      transition: transform 0.2s;
    }
  }
}

.notification-section {
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid #e0e0e0;

  .section-title {
    margin-bottom: 15px;
  }
}

.timeline-container {
  height: 300px;
  overflow-y: auto;
  padding-right: 10px;

  &::-webkit-scrollbar {
    width: 6px;
  }

  &::-webkit-scrollbar-thumb {
    background-color: #c1c1c1;
    border-radius: 3px;
  }

  .timeline-item {
    padding-bottom: 15px;

    :deep(.el-timeline-item__timestamp) {
      color: #64748b;
      font-size: 13px;
      font-weight: bold;
    }
  }

  .notification-item {
    background-color: #f8fafc;
    padding: 10px 15px;
    border-radius: 6px;
    border-left: 3px solid #a5b4fc;
  }
}

.notice-dialog {
  :deep(.el-dialog__header) {
    border-bottom: 1px solid #e0e0e0;
    margin-right: 0;
  }

  :deep(.el-input__inner) {
    border-radius: 6px;
  }

  :deep(.el-textarea__inner) {
    border-radius: 6px;
    min-height: 100px !important;
  }
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;

  .cancel-btn {
    &:hover {
      color: #4f46e5;
      border-color: #c7d2fe;
    }
  }

  .confirm-btn {
    background-color: #4f46e5;

    &:hover {
      background-color: #4338ca;
    }
  }
}

.add-btn {
  background-color: #4f46e5;
  border: none;

  &:hover {
    background-color: #4338ca;
  }
}

@media (max-width: 768px) {
  .el-col {
    width: 100%;
    padding: 0 !important;
    margin-bottom: 20px;

    &.left-column {
      border-right: none;
      border-bottom: 1px solid #e0e0e0;
      padding-bottom: 20px !important;
    }
  }

  .left-container, .timeline-container {
    height: auto;
    max-height: 400px;
  }
}
</style>
